package com.wsx.first.c;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author: wangshunxi
 * @date: 2022/5/10 16:22
 * @Description:
 */
public class Main0043 {
    public static class Point {
        public int x;
        public int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public static void main(String[] args) {


        getInts();





    }

    private static void getInts() {
        try (Scanner scanner = new Scanner(System.in)) {
            while (scanner.hasNext()) {
                int x = scanner.nextInt();
                int y = scanner.nextInt();
                int[][] maze = new int[x][y];
                for (int i = 0; i < x; i++) {
                    for (int j = 0; j < y; j++) {
                        maze[i][j] = scanner.nextInt();
                    }
                }
                ArrayList<Point> result = new ArrayList<>();
                dfs(maze, 0, 0, result);
                for (Point p : result) {
                    System.out.println("(" + p.x + "," + p.y + ")");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    private static int[][] getInts2() {
        return new int[][]{
                {0, 1, 0, 0, 0},
                {0, 1, 1, 1, 0},
                {0, 0, 0, 0, 0},
                {0, 1, 1, 1, 0},
                {0, 0, 0, 1, 0}
        };
    }

    private static boolean dfs(int[][] map, int x, int y, List<Point> path) {
        int[][] d = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        path.add(new Point(x, y));
        map[x][y] = 1;
        //到终点了
        if (x == map.length- 1 && y == map[0].length - 1) {
            return true;
        }
        int xMax = map.length - 1;
        int yMax = map[0].length - 1;
        //每个方向尝试
        for (int i = 0; i < d.length; i++) {
            int xx = x + d[i][0];
            int yy = y + d[i][1];
            if (xx >= 0 && xx <= xMax && yy >= 0 && yy <= yMax) {
                if (map[xx][yy] == 0) {
                    //递归尝试
                    if (dfs(map, xx, yy, path)) {
                        return true;
                    }
                }
            }
        }
        path.remove(path.size() - 1);
        map[x][y] = 0;
        return false;
    }


}
